package com.nf.practice.readHtmlText;

import com.dingtalk.api.request.OapiAtsPluginDataDeleteRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ReadHtmlText {

    // 保存区域版块
    @Test
    public void testArea() throws Exception {

        Document doc1 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\中国500强2023.html"), "utf8");
        Document doc2 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\世界500强2022.html"), "utf8");

        Document doc3 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\中国500强2022.html"), "utf8");
        Document doc4 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\世界500强2021.html"), "utf8");

        Document doc5 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\中国500强2021.html"), "utf8");
        Document doc6 = Jsoup.parse(new File("E:\\风险研发\\20230731初筛数据源调用逻辑优化\\世界500强2020.html"), "utf8");

        Elements rows1 = doc1.select("a");
        Elements rows2 = doc2.select("a");
        Elements rows3 = doc3.select("a");
        Elements rows4 = doc4.select("a");
        Elements rows5 = doc5.select("a");
        Elements rows6 = doc6.select("a");

        List<String> result = new ArrayList<>();

        getTop500Union(result, rows1);
        getTop500Union(result, rows2);
        getTop500Union(result, rows3);
        getTop500Union(result, rows4);
        getTop500Union(result, rows5);
        getTop500Union(result, rows6);
        System.out.println(result);

    }

    public void getTop500Union(List<String> result, Elements rows) {
        for (Element row : rows) {
            Elements cells = row.select("a");
            if (cells.size() > 0) {
                String companyName = cells.get(0).text();
                if (companyName.contains("（")) {
                    companyName = companyName.substring(0,companyName.indexOf("（"));
                    companyName = companyName.replace(" ","");
                }
                if (!result.contains(companyName)) {
                    result.add(companyName);
                }
            }
        }
    }

    @Test
    public void getRatio() {
        String str1 = "英国法通保险";
        String str2 = "英国法通保险校";
        float similarity = getSimilarityRatio(str1,str2);
//        double similarity = (1.0 * levenshteinDistance(str1, str2)) / Math.max(str1.length(), str2.length());
        System.out.println(similarity);
    }

    public static int levenshteinDistance(String s1, String s2) {
        int[][] dp = new int[s1.length() + 1][s2.length() + 1];

        for (int i = 0; i <= s1.length(); i++) {
            dp[i][0] = i;
        }

        for (int j = 0; j <= s2.length(); j++) {
            dp[0][j] = j;
        }

        for (int i = 1; i <= s1.length(); i++) {
            for (int j = 1; j <= s2.length(); j++) {
                if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                }
            }
        }

        return dp[s1.length()][s2.length()];
    }


    public float getSimilarityRatio(String str, String target) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str) || org.apache.commons.lang3.StringUtils.isBlank(target)) {
            return 0;
        }
        // 去除空白字符、换行、标点符号
        String regex = "[\\pP\\p{Punct}\\s]";
        return (1 - (float) compare(str.replaceAll(regex, ""), target.replaceAll(regex, ""))
                / Math.max(str.length(), target.length()));
    }

    private int compare(String str, String target) {
        int d[][]; // 矩阵
        int n = str.length();
        int m = target.length();
        int i; // 遍历str的
        int j; // 遍历target的
        char ch1; // str的
        char ch2; // target的
        int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
        if (n == 0) {
            return m;
        }
        if (m == 0) {
            return n;
        }
        d = new int[n + 1][m + 1];
        for (i = 0; i <= n; i++) { // 初始化第一列
            d[i][0] = i;
        }

        for (j = 0; j <= m; j++) { // 初始化第一行
            d[0][j] = j;
        }

        for (i = 1; i <= n; i++) { // 遍历str
            ch1 = str.charAt(i - 1);
            // 去匹配target
            for (j = 1; j <= m; j++) {
                ch2 = target.charAt(j - 1);
                if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
                    temp = 0;
                } else {
                    temp = 1;
                }
                // 左边+1,上边+1, 左上角+temp取最小
                d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
            }
        }
        return d[n][m];
    }

    private int min(int one, int two, int three) {
        return (one = one < two ? one : two) < three ? one : three;
    }

}
